package com.xiaoyuan.minicore.tx;

import java.lang.reflect.Method;

import com.xiaoyuan.minicore.aop.DefaultHandler;
import com.xiaoyuan.minicore.dao.DBHelper;
import com.xiaoyuan.minicore.tx.annotation.Transaction;

/**
 * @Auther: xiaoyuan
 * @Date: 2020/4/30
 * @Description:
 */
public class TxHanlder extends DefaultHandler {

    @Override
    public boolean filter(Method method, Object[] args) {
        if(method.isAnnotationPresent(Transaction.class)){
            return true;
        }
        return false;
    }

    @Override
    public void before(Method method, Object[] args) {
        // 开启事务
        DBHelper.beginTransaction();
    }

    @Override
    public void after(Method method, Object[] args) {
        // 提交事务
        DBHelper.commitTransaction();
    }

    @Override
    public void error(Method method, Object[] args, Exception e) {
        // 回滚事务
        DBHelper.rollbackTransaction();
    }

}
